#!./jsrun

var html = '
<html>
<body>
</body>
</html>
';

var Text = function(x) {
	this.nodeValue = x;
}
Text.prototype = {
	getInnerHTML: function() {
		return this.nodeValue;
	},
	getOuterHTML: function() {
		return this.nodeValue;
	}
};

var HTMLElement = function(x) {
	if(x !== undefined)
		this.tagName = x;
	this.attributes = {};
	this.children = [];
	this.children.toString = function(){ return '[HTMLCollection]' };
	this.parent = null;
};

HTMLElement.prototype = {
	setAttribute: function(name, value) {
		this.attributes[name] = value;
	},
	getAttribute: function(name) {
		return this.attributes[name];
	},
	appendChild: function(elem) {
		this.children[this.children.length] = elem;
		elem.parentNode = this;
		return elem;
	},
	getInnerHTML: function() {
		var out = [];
		for (var i=0, max=this.children.length; i < max; ++i)
			out[out.length] = this.children[i].getOuterHTML();
		return out.join('');
	},
	getOuterHTML: function() {
		var out = [];
		out[out.length] = '<' + this.tagName;
		for(var i in this.attributes) {
			out[out.length] = ' ' + i + '=' + this.attributes[i]
		}
		out[out.length] = '>';
		out[out.length] = this.getInnerHTML();
		out[out.length] = '</' + this.tagName + '>';
		return out.join('');
	},
	toString: function() {
		return '[HTMLElement ' + this.tagName + ']';
	},
};

var HTMLDocument = function() {
	this.documentElement = new HTMLElement('html');
	this.documentElement.ownerDocument = this;
};
HTMLDocument.prototype = {
	createElement: function(x) {
		alert("createElement: " + x);
		var elem = new HTMLElement();
		elem.tagName = x;
		elem.ownerDocument = this;
		return elem;
	},
	createTextNode: function(data) {
		return new Text(data);
	},
};

var document = new HTMLDocument();
dom_parse(html, document.documentElement);

alert(document);


/*
var head = document.documentElement.appendChild(document.createElement('head'));
var body = document.createElement('body');
document.documentElement.appendChild(body);

document.documentElement.setAttribute('class', 'js');

var h1 = document.createElement('h1');
h1.appendChild(document.createTextNode('privet'));
body.appendChild(h1);

alert(document.documentElement.getOuterHTML());
*/




